package mi.payment.ui.admin;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.List;

import mi.payment.R;
import mi.payment.controller.BaseQRCodeTransactionController;
import mi.payment.controller.QRCodeTransactionController;
import mi.payment.settings.TransactionSwitchOperation;
import mi.payment.settings.TransactionSwitchSetting;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.title.Title;

/**
 * 扫码类交易控制
 * Created by zdy on 2017/8/23.
 */

public class QRCodeTransactionActivity extends ParentActivity {
    private View weChatScan, weChatQR, aliPayScan, aliPayQR, kouBei, unionPayScan, unionPayQR;
    private ButtonSwitch weChatScanButton, weChatQRButton, aliPayScanButton, aliPayQRButton, kouBeiButton, unionPayScanButton, unionPayQRButton;
    private BaseQRCodeTransactionController controller;
    private TransactionSwitchOperation operation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_code_transaction);
        controller = new QRCodeTransactionController();
        operation = TransactionSwitchOperation.getInstance();
        initView();
        visibleViews();
        setClickListener();
        setCheckChangeListener();
        updateUI();
    }

    private void initView() {
        Title mTitle;
        TextView mLeftText;
        mTitle = (Title) findViewById(R.id.qr_code_transaction_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.qr_code_transaction_switch_settings));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QRCodeTransactionActivity.this.finish();
            }
        }, null, null, null);

        //微信主扫
        weChatScan = findViewById(R.id.qr_code_we_chat_scan);

        //微信被扫
        weChatQR = findViewById(R.id.qr_code_we_chat_qr);

        //支付宝主扫
        aliPayScan = findViewById(R.id.qr_code_alipay_scan);

        //支付宝被扫
        aliPayQR = findViewById(R.id.qr_code_alipay_qr);

        //口碑支付
        kouBei = findViewById(R.id.qr_code_kou_bei);

        //银联扫码主扫
        unionPayScan = findViewById(R.id.qr_code_union_pay_scan);

        //银联扫码被扫
        unionPayQR = findViewById(R.id.qr_code_union_pay_qr);

        mLeftText = (TextView) weChatScan.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.wechat) + getResources().getString(R.string.qr_code_scan));

        mLeftText = (TextView) weChatQR.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.wechat) + getResources().getString(R.string.qr_code_qr));


        mLeftText = (TextView) aliPayScan.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.alipay) + getResources().getString(R.string.qr_code_scan));

        mLeftText = (TextView) aliPayQR.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.alipay) + getResources().getString(R.string.qr_code_qr));

        mLeftText = (TextView) kouBei.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.qr_code_kou_bei));

        mLeftText = (TextView) unionPayScan.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.qr_code_union_pay) + getResources().getString(R.string.qr_code_scan));

        mLeftText = (TextView) unionPayQR.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.qr_code_union_pay) + getResources().getString(R.string.qr_code_qr));


        weChatScanButton = (ButtonSwitch) weChatScan.findViewById(R.id.set_switch);
        weChatQRButton = (ButtonSwitch) weChatQR.findViewById(R.id.set_switch);
        aliPayScanButton = (ButtonSwitch) aliPayScan.findViewById(R.id.set_switch);
        aliPayQRButton = (ButtonSwitch) aliPayQR.findViewById(R.id.set_switch);
        kouBeiButton = (ButtonSwitch) kouBei.findViewById(R.id.set_switch);
        unionPayScanButton = (ButtonSwitch) unionPayScan.findViewById(R.id.set_switch);
        unionPayQRButton = (ButtonSwitch) unionPayQR.findViewById(R.id.set_switch);

    }


    /**
     * 设置需要显示的View
     */

    private void visibleViews() {
        List<Integer> tagList = controller.getVisibleViews();
        for (Integer tag : tagList) {
            switch (tag) {
                case BaseQRCodeTransactionController.WE_CHAT_SCAN:
                    weChatScan.setVisibility(View.VISIBLE);
                    break;
                case BaseQRCodeTransactionController.WE_CHAT_QR:
                    weChatQR.setVisibility(View.VISIBLE);
                    break;
                case BaseQRCodeTransactionController.ALI_PAY_SCAN:
                    aliPayScan.setVisibility(View.VISIBLE);
                    break;
                case BaseQRCodeTransactionController.ALI_PAY_QR:
                    aliPayQR.setVisibility(View.VISIBLE);
                    break;
                case BaseQRCodeTransactionController.KOU_BEI:
                    kouBei.setVisibility(View.VISIBLE);
                    break;
                case BaseQRCodeTransactionController.UNION_PAY_SCAN:
                    unionPayScan.setVisibility(View.VISIBLE);
                    break;
                case BaseQRCodeTransactionController.UNION_PAY_QR:
                    unionPayQR.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }
        }
    }


    /**
     * 设置点击事件监听
     */
    private void setClickListener() {
        weChatScan.setOnClickListener(this);
        weChatQR.setOnClickListener(this);
        aliPayScan.setOnClickListener(this);
        aliPayQR.setOnClickListener(this);
        kouBei.setOnClickListener(this);
        unionPayScan.setOnClickListener(this);
        unionPayQR.setOnClickListener(this);
    }


    private void setCheckChangeListener() {
        weChatScanButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                operation.saveWeChatScan(isChecked);
            }
        });

        weChatQRButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                operation.saveWeChatQR(isChecked);
            }
        });

        aliPayScanButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                operation.saveAliPayScan(isChecked);
            }
        });

        aliPayQRButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                operation.saveAliPayQR(isChecked);
            }
        });

        kouBeiButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                operation.saveKouBei(isChecked);
            }
        });

        unionPayScanButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                operation.saveUnionPayScan(isChecked);
            }
        });

        unionPayQRButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                operation.saveUnionPayQR(isChecked);
            }
        });

        updateUI();
    }

    private void updateUI() {
        TransactionSwitchSetting mTransactionSwitchSetting = operation.getTransactionSwitchSetting();
        weChatScanButton.setOnCheck(mTransactionSwitchSetting.isUseWeChatScan());
        weChatQRButton.setOnCheck(mTransactionSwitchSetting.isUseWeChatQR());
        aliPayScanButton.setOnCheck(mTransactionSwitchSetting.isUseAliPayScan());
        aliPayQRButton.setOnCheck(mTransactionSwitchSetting.isUseAliPayQR());
        kouBeiButton.setOnCheck(mTransactionSwitchSetting.isUseKouBei());
        unionPayScanButton.setOnCheck(mTransactionSwitchSetting.isUseUnionPayScan());
        unionPayQRButton.setOnCheck(mTransactionSwitchSetting.isUseUnionPayQR());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qr_code_we_chat_scan:
                weChatScanButton.setOnCheck(!weChatScanButton.isChecked());
                break;
            case R.id.qr_code_we_chat_qr:
                weChatQRButton.setOnCheck(!weChatQRButton.isChecked());
                break;
            case R.id.qr_code_alipay_scan:
                aliPayScanButton.setOnCheck(!aliPayScanButton.isChecked());
                break;
            case R.id.qr_code_alipay_qr:
                aliPayQRButton.setOnCheck(!aliPayQRButton.isChecked());
            case R.id.qr_code_kou_bei:
                kouBeiButton.setOnCheck(!kouBeiButton.isChecked());
                break;
            case R.id.qr_code_union_pay_scan:
                unionPayScanButton.setOnCheck(!unionPayScanButton.isChecked());
                break;
            case R.id.qr_code_union_pay_qr:
                unionPayQRButton.setOnCheck(!unionPayQRButton.isChecked());
                break;
            default:
                break;
        }
    }
}
